1
|
|
|
/*! |
2
|
|
|
* @package ElkArte Forum |
3
|
|
|
* @copyright ElkArte Forum contributors |
4
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
5
|
|
|
* |
6
|
|
|
* @version 2.0 dev |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This function changes the relative time around the page real-timeish |
11
|
|
|
*/ |
12
|
|
|
function updateRelativeTime () |
13
|
|
|
{ |
14
|
|
|
const timeElements = document.querySelectorAll('time'); |
15
|
|
|
|
16
|
|
|
let relative_time_refresh = 3600000; |
17
|
|
|
|
18
|
|
|
timeElements.forEach(function(timeElement) { |
19
|
|
|
let oRelativeTime = new relativeTime(timeElement.getAttribute('data-timestamp') * 1000, oRttime.referenceTime), |
|
|
|
|
20
|
|
|
time_text = ''; |
21
|
|
|
|
22
|
|
|
if (oRelativeTime.seconds()) |
23
|
|
|
{ |
24
|
|
|
timeElement.textContent = oRttime.now; |
25
|
|
|
relative_time_refresh = Math.min(relative_time_refresh, 10000); |
26
|
|
|
} |
27
|
|
|
else if (oRelativeTime.minutes()) |
28
|
|
|
{ |
29
|
|
|
time_text = oRelativeTime.deltaTime > 1 ? oRttime.minutes : oRttime.minute; |
30
|
|
|
timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
31
|
|
|
relative_time_refresh = Math.min(relative_time_refresh, 60000); |
32
|
|
|
} |
33
|
|
|
else if (oRelativeTime.hours()) |
34
|
|
|
{ |
35
|
|
|
time_text = oRelativeTime.deltaTime > 1 ? oRttime.hours : oRttime.hour; |
36
|
|
|
timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
37
|
|
|
relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
38
|
|
|
} |
39
|
|
|
else if (oRelativeTime.days()) |
40
|
|
|
{ |
41
|
|
|
time_text = oRelativeTime.deltaTime > 1 ? oRttime.days : oRttime.day; |
42
|
|
|
timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
43
|
|
|
relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
44
|
|
|
} |
45
|
|
|
else if (oRelativeTime.weeks()) |
46
|
|
|
{ |
47
|
|
|
time_text = oRelativeTime.deltaTime > 1 ? oRttime.weeks : oRttime.week; |
48
|
|
|
timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
49
|
|
|
relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
50
|
|
|
} |
51
|
|
|
else if (oRelativeTime.months()) |
52
|
|
|
{ |
53
|
|
|
time_text = oRelativeTime.deltaTime > 1 ? oRttime.months : oRttime.month; |
54
|
|
|
timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
55
|
|
|
relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
56
|
|
|
} |
57
|
|
|
else if (oRelativeTime.years()) |
58
|
|
|
{ |
59
|
|
|
time_text = oRelativeTime.deltaTime > 1 ? oRttime.years : oRttime.year; |
60
|
|
|
timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
61
|
|
|
relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
62
|
|
|
} |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
oRttime.referenceTime += relative_time_refresh; |
|
|
|
|
66
|
|
|
|
67
|
|
|
setTimeout(function() { |
68
|
|
|
updateRelativeTime(); |
69
|
|
|
}, relative_time_refresh); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Function/object to handle relative times |
74
|
|
|
* |
75
|
|
|
* sTo is optional, if omitted the relative time is calculated from sFrom up to "now" |
76
|
|
|
* |
77
|
|
|
* @param {int} sFrom |
78
|
|
|
* @param {int} sTo |
79
|
|
|
*/ |
80
|
|
|
function relativeTime (sFrom, sTo) |
81
|
|
|
{ |
82
|
|
|
// helper function to reduce code repetition |
83
|
|
|
const createDate = (s) => { |
84
|
|
|
let date = new Date(s); |
85
|
|
|
|
86
|
|
|
if (isNaN(date.getTime())) |
87
|
|
|
{ |
88
|
|
|
const sSplit = s.split(/\D/); |
89
|
|
|
date = new Date(sSplit[0], --sSplit[1], sSplit[2], sSplit[3], sSplit[4]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return date; |
93
|
|
|
}; |
94
|
|
|
|
95
|
|
|
try |
96
|
|
|
{ |
97
|
|
|
this.dateFrom = createDate(sFrom); |
98
|
|
|
this.dateTo = sTo ? createDate(sTo) : new Date(); |
99
|
|
|
if (isNaN(this.dateFrom.getTime()) || isNaN(this.dateTo.getTime())) |
100
|
|
|
{ |
101
|
|
|
throw new Error('Invalid date'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
catch (error) |
105
|
|
|
{ |
106
|
|
|
if ('console' in window && console.error) |
107
|
|
|
{ |
108
|
|
|
console.error('Invalid date provided', error); |
109
|
|
|
} |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
this.past_time = (this.dateTo - this.dateFrom) / 1000; |
114
|
|
|
this.deltaTime = 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
relativeTime.prototype.seconds = function() { |
118
|
|
|
// Within the first 60 seconds it is just now. |
119
|
|
|
if (this.past_time < 60) |
120
|
|
|
{ |
121
|
|
|
this.deltaTime = this.past_time; |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
}; |
127
|
|
|
|
128
|
|
|
relativeTime.prototype.minutes = function() { |
129
|
|
|
// Within the first hour? |
130
|
|
|
if (this.past_time >= 60 && Math.round(this.past_time / 60) < 60) |
131
|
|
|
{ |
132
|
|
|
this.deltaTime = Math.round(this.past_time / 60); |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
}; |
138
|
|
|
|
139
|
|
|
relativeTime.prototype.hours = function() { |
140
|
|
|
// Some hours but less than a day? |
141
|
|
|
if (Math.round(this.past_time / 60) >= 60 && Math.round(this.past_time / 3600) < 24) |
142
|
|
|
{ |
143
|
|
|
this.deltaTime = Math.round(this.past_time / 3600); |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
}; |
149
|
|
|
|
150
|
|
|
relativeTime.prototype.days = function() { |
151
|
|
|
// Some days ago but less than a week? |
152
|
|
|
if (Math.round(this.past_time / 3600) >= 24 && Math.round(this.past_time / (24 * 3600)) < 7) |
153
|
|
|
{ |
154
|
|
|
this.deltaTime = Math.round(this.past_time / (24 * 3600)); |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return false; |
159
|
|
|
}; |
160
|
|
|
|
161
|
|
|
relativeTime.prototype.weeks = function() { |
162
|
|
|
// Weeks ago but less than a month? |
163
|
|
|
if (Math.round(this.past_time / (24 * 3600)) >= 7 && Math.round(this.past_time / (24 * 3600)) < 30) |
164
|
|
|
{ |
165
|
|
|
this.deltaTime = Math.round(this.past_time / (24 * 3600) / 7); |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
}; |
171
|
|
|
|
172
|
|
|
relativeTime.prototype.months = function() { |
173
|
|
|
// Months ago but less than a year? |
174
|
|
|
if (Math.round(this.past_time / (24 * 3600)) >= 30 && Math.round(this.past_time / (30 * 24 * 3600)) < 12) |
175
|
|
|
{ |
176
|
|
|
this.deltaTime = Math.round(this.past_time / (30 * 24 * 3600)); |
177
|
|
|
return true; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return false; |
181
|
|
|
}; |
182
|
|
|
|
183
|
|
|
relativeTime.prototype.years = function() { |
184
|
|
|
// Oha, we've passed at least a year? |
185
|
|
|
if (Math.round(this.past_time / (30 * 24 * 3600)) >= 12) |
186
|
|
|
{ |
187
|
|
|
this.deltaTime = this.dateTo.getFullYear() - this.dateFrom.getFullYear(); |
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return false; |
192
|
|
|
}; |
193
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.